package com.sqlweb.sqlweb.base;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.InputStreamReader;

@Slf4j
public class CommandLineR {
    public static void main(String[] args) {
        try {
            // 获取R脚本的完整路径
            String scriptPath = "src/main/resources/fun.R"; // 确保路径正确

            // 调用 Rscript 来执行 R 脚本
            ProcessBuilder processBuilder = new ProcessBuilder("C:\\Program Files\\R\\R-4.4.2\\bin\\Rscript.exe", scriptPath);
            processBuilder.redirectErrorStream(true); // 合并错误输出

            Process process = processBuilder.start();

            // 读取输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                log.info("执行结果:" + line);
            }

            // 等待进程完成
            int exitCode = process.waitFor();
            log.info("R script exited with code " + exitCode);

        } catch (Exception e) {
            log.error("调用失败：" + e);
        }
    }
}
